package main.java.com.lee.thread;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.concurrent.CountDownLatch;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2021/12/6
 */
public class InterruptTest {
	public static void main(String[] args) throws InterruptedException {
		testInterruptFirst();
//		testInBlock();
//		inRunning();

	}

	private static void testInterruptFirst() throws InterruptedException {
		Thread thread = new Thread() {
			@Override
			public void run() {
				try {
					System.out.println(LocalDateTime.now());
					Thread.currentThread().interrupt();

					Thread.sleep(5000);
					System.out.println("I am after sleep1");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				try {
					System.out.println(LocalDateTime.now());
					Thread.sleep(5000);
					System.out.println("I am after sleep2");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		thread.start();
		thread.join();
	}

	private static void testInBlock() throws InterruptedException {
		Thread thread = new Thread() {
			@Override
			public void run() {
				System.out.println(LocalDateTime.now());
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}
		};
		thread.start();
		Thread.sleep(2000);
		thread.interrupt();
		thread.join();
		System.out.println(LocalDateTime.now());
	}

	private static void inRunning() throws InterruptedException {
		Thread thread = new Thread() {
			@Override
			public void run() {
				System.out.println("运行中");
			}
		};
		thread.start();
		System.out.println(thread.isInterrupted());
		thread.interrupt();
		System.out.println(thread.isInterrupted());
		thread.join();
	}
}
